package etxt2db.creator;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import etxt2db.utils.AnalysisEngineDescriptorData;
import etxt2db.utils.AnalysisEngineDescriptorWriter;

/**
 * A creator of UCompare components, wrapping E-txt2db models. 
 * The components are materialized by UIMA Analysis Engine XML descriptors.
 * <p>
 * 
 * @author Gonçalo Simões
 * @author Rui Lageira
 *
 */
public class ComponentCreator {

	/**
	 * Writes to disk an UIMA Analysis Engine XML descriptor 
	 * with references to the E-txt2db model, attributes to 
	 * find and input types to be accepted by the UCompare component.
	 * <p>
	 * @param modelName		the name of the E-txt2db model to wrap
	 * @param destPath		the destination directory in which the XML descriptor is saved
	 * @param attributes	the list of attributes to find
	 * @param inputs		the list of types to be accepted by the UCompare component
	 * @param mappingsContent	the list of lines of the type mappings configuration file
	 * @throws ParserConfigurationException	if a problem with XML writing occurs in {@link AnalysisEngineDescriptorWriter#write(AnalysisEngineDescriptorData, String)}
	 * @throws TransformerException			if a problem with XML writing occurs in {@link AnalysisEngineDescriptorWriter#write(AnalysisEngineDescriptorData, String)}
	 * @throws IOException					if an input or output exception occurs in {@link AnalysisEngineDescriptorWriter#write(AnalysisEngineDescriptorData, String)}
	 */
	public void generateComponentFromModel(String modelName, String destPath, List<String> attributes, List<String> inputs, List<String> mappingsContent) throws ParserConfigurationException, TransformerException, IOException {
		
		AnalysisEngineDescriptorData data = new AnalysisEngineDescriptorData();
		
		String componentName = "Etxt2DB" + modelName + "Component";
		String relativeModelPath = destPath+(destPath.endsWith("/") ? "" : "/");
		String absoluteModelPath = System.getProperty("user.dir")+"/"+relativeModelPath+modelName+".ann";
		
		data.setName(componentName);
		data.setModelPath(absoluteModelPath);
		data.setAttributes(attributes);
		data.setInputTypes(inputs);
		data.setMappingsFileContent(mappingsContent);
		
		AnalysisEngineDescriptorWriter.write(data,destPath);
		
		System.out.println("Component generated!");
		
	}

}
